<?php

/**
 * Profile controller
 * Delegates control to profile controllers to seperate the distinct profile features
 */
class Profilecontroller {

	public  $currentSelectedProfile;

	/**
	 * Constructor
	 * @param Object $registry the registry
	 * @param bool $directCall
	 */
	public function __construct( $registry){
		$this->registry = $registry;

		$urlBits = $this->registry->getObject('url')->getURLBits();
		$defaultProfileID = $registry->getObject('authenticate')->getUser()->getCurrentProfile()->getProfileID();

		if (isset($_POST['profile_id']) && intval($_POST['profile_id']) > 0)
		{
			$defaultProfileID = $_POST['profile_id'];
		}

		$action = '';
		if (true == isset($urlBits[1]))
		$action = $urlBits[1];
			
		$userID = $this->registry->getObject('authenticate')->getUser()->getUserID();
		$profileID = $defaultProfileID;

		if ($action == 'home'){
			if ( (isset($urlBits[2]) && intval($urlBits[2]) > 0)&& (isset($urlBits[3]) && intval($urlBits[3]) > 0)){
				$userID = intval($urlBits[2]);
				$profileID = intval($urlBits[3]);
			}
		}

		$profile = $this->getProfile($userID, $profileID);
		if (true == $profile->getIsInvalidProfile()){
			if ('create' != $action){
				$profileID = 1;
				$profile = $this->getProfile($userID, $profileID);
			}
		}

		switch( $action )
		{
			case 'view':
			case 'edit':
			case 'create':
				$this->staticContentDelegator( $userID, $profileID, $profile );
				break;
			default:
				$this->statusesDelegator( $userID, $profileID, $profile );
				break;
		}
	}

	/**
	 * Delegate control to the static content profile controller
	 * @param int $user the user whose profile we are viewing
	 * @return void
	 */
	private function staticContentDelegator( $user, $profileID, $profile ){
		$this->checkProfile( $profile );
		require_once( FRAMEWORK_PATH . 'controllers/profile/profileinformationcontroller.php' );
		$sc = new Profileinformationcontroller( $this->registry, true, $user, $profileID );
	}

	/**
	 * Delegate control to the statuses profile controller
	 * @param int $user the user whose profile we are viewing
	 * @return void
	 */
	private function statusesDelegator( $user, $profileid, $profile ){
		$profileChanged = $this->checkProfile( $profile );
		require_once( FRAMEWORK_PATH . 'controllers/profile/profilestatusescontroller.php' );
		$sc = new Profilestatusescontroller( $this->registry, $user, $this->currentSelectedProfile ,$profileChanged, $profile);
	}

	/**
	 * Display an error - you cannot access profiles simply by visiting /profile/ !
	 * @return void
	 */
	private function profileError(){
		$this->registry->errorPage( 'Sorry, an error has occured', 'The link you followed was invalid, please try again');
	}

	/**
	 *
	 * Get profile
	 * @param int $userID
	 * @param int $profileID
	 * @return profile
	 */
	private function getProfile($userID, $profileID){
		require_once( FRAMEWORK_PATH . 'models/profile.php' );
		$profile = new Profile( $this->registry, $userID, $profileID  );
		return $profile;
	}

	/**
	 *
	 * Check for the profile ownership
	 * @param $user - userid
	 * @param $profileID - profileid
	 * @return int 0 - loggedin userid and no profilechange
	 * 			   1 - loggedin userid but profile changed
	 * 			   2 - Different userid and different profileid (i.e viewing another users information)
	 */
	private function checkProfile( $profile )
	{
		$profileChanged = 0;
		$this->currentSelectedProfile = $profile;

		// Different user
		if ($profile->getUserID() != $this->registry->getObject('authenticate')->getUser()->getUserID())
		{
			$profileChanged = 2;
		}
		else if ($profile->getProfileID() != $this->registry->getObject('authenticate')->getUser()->getCurrentProfile()->getProfileID())
		{
			$profileChanged = 1;
			$this->registry->getObject('authenticate')->getUser()->setCurrentProfile($profile);
			$_SESSION['sn_current_profile_id'] = $profile->getProfileID();
		}
		else
		{
			$profileChanged = 0;
		}

		$_SESSION['current_view_userID'] = $profile->getUserID();
		$_SESSION['current_view_ProfileID'] = $profile->getProfileID();

		return $profileChanged;
	}
}

?>

